package com.yuer.zk.zk;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

import java.nio.charset.StandardCharsets;

/**
 * curator的应用实例
 */
public class ZkCuratorCreateNodeTest {


    public static void main(String[] args) throws Exception {
        ExponentialBackoffRetry exponentialBackoffRetry = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient("192.168.56.103:2182", exponentialBackoffRetry);
        curatorFramework.start();
        System.out.println("exponentialBackoffRetry:   "+curatorFramework.getState());


        ExponentialBackoffRetry exponentialBackoffRetry2 = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework curatorFramework2 = CuratorFrameworkFactory.newClient("192.168.56.103:2182",5000,1000, exponentialBackoffRetry2);
        curatorFramework2.start();
        System.out.println("curatorFramework2:   "+curatorFramework2.getState());


        CuratorFramework yuerbaseCurator = CuratorFrameworkFactory.builder()
                .connectString("192.168.56.103:2182")
                .sessionTimeoutMs(2000)
                .connectionTimeoutMs(1000)
                .retryPolicy(exponentialBackoffRetry2)
                .namespace("yuerbase")
                .build();
        yuerbaseCurator.start();
        System.out.println("yuerbaseCurator: "+ yuerbaseCurator.getState());

        System.out.println("namespace:  "+ curatorFramework.getNamespace());

        String parentPath = "/seconed/C1";
        Stat stat1 = curatorFramework.checkExists().forPath(parentPath);
        Stat stat2 = curatorFramework.checkExists().forPath("/parentPath");
        if(stat1 != null){
            System.out.println(parentPath+"节点已经存在！");
            curatorFramework.delete().deletingChildrenIfNeeded().forPath(parentPath);
//             curatorFramework.delete().forPath(parentPath);
        }


//        Stat stat = curatorFramework.create().forPath(parentPath);


        // 创建节点
        String s = curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(parentPath);
        Thread.sleep(1000);
        System.out.println(s);

        curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT)
                .forPath(parentPath+"/child","childContent".getBytes());

        //获取节点的数据
        Stat stat = new Stat();
        byte []   createContentByte  = curatorFramework.getData().storingStatIn(stat).forPath(parentPath+"/child");
        System.out.println("详细信息：");
        System.out.println(stat);
        System.out.println("内容1： " +new String(createContentByte));
        System.out.println("内容的长度： " + stat.getDataLength()+"  " + new String(createContentByte).length());

        //节点的内容更改
        curatorFramework.setData().forPath(parentPath+"/child","这是更新之后的内容".getBytes(StandardCharsets.UTF_8));

        byte []  updataContent = curatorFramework.getData().forPath(parentPath+"/child");
        System.out.println("这是更新之后的内容：  "+new String(updataContent));

        // 删除
        curatorFramework.delete().deletingChildrenIfNeeded().forPath(parentPath+"/child");

        //检查节点是否存在
        stat = curatorFramework.checkExists().forPath(parentPath+"/child");

        if(stat != null){
            System.out.println("该节点没有被删除");
        }else {
            System.out.println("该节点被删除");
        }
    }


    public static void main1111111123123421412(String[] args) {
        int sum = 0;
        for (int a = 1; a <= 10; a++) {
            sum += a;
            System.out.print(sum);
            System.out.print("\t");
//            int lastIndex = a+1;
//            System.out.print(lastIndex);
//            System.out.print("\t");
        }
    }
}
